package ThreadPoolswithExecutorFramework_7_chapt;

import java.util.ArrayList;
import java.util.List;



/*
* The base means for concurrency are is the java.lang.Threads class. 
* A Thread executes an object of type java.lang.Runnable.
* */


public class MyRunnable implements Runnable {

	private final long countUntil;
	
	
	public MyRunnable(long countUntil) {
		this.countUntil=countUntil;
		
	}
	
	@Override
	public void run() {
		long sum=0;
		for(long i=0;i<countUntil;i++){
			sum+=i;
		}
		System.out.println(sum);
	}

	
	
	
	//////////////// main
	
	public static void main(String[] args) {
	
		List<Thread> threads=new ArrayList<>();

		for(int i=0;i<500;i++){
			Runnable task=new MyRunnable(1000000L+i);
			Thread worker=new Thread(task);
			worker.setName(String.valueOf(i));
			worker.start();
			threads.add(worker);
		}
		
		int running=0;
		do{
			running=0;
			for(Thread thread:threads){
				if(thread.isAlive()) running++;
			}
			
			System.out.println("We have "+running+" running threads");
		}while(running>0);
	}
}

/*
 * The java.util.concurrent package offers improved support for concurrency compared to the
 *  direct usage of Threads.
 *  This package is described in the next section.
 * 
 * */





// start - to start thread (run - is simple method)